package net.godideas.giframework.widget;

import java.util.Arrays;
import java.util.List;

import net.godideas.giframework.R;
import net.godideas.giframework.mvp.BaseDialog;
import net.godideas.giframework.view.CommonEventListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

public class CMenu extends BaseDialog {

	private CMenuContent mMenuContent;
	private CommonEventListener mCommonEventListener;

	/**
	 * Set CommonEventListener
	 * 
	 * @param value
	 */
	public void setCommonEventListener(CommonEventListener value) {
		mCommonEventListener = value;
		mMenuContent.setCommonEventListener(mCommonEventListener);
	}

	public void computePosition(int x, int y) {
		int gravityVert = Gravity.TOP, gravityHorz = Gravity.LEFT;

		int maxWidth = getContext().getResources().getDisplayMetrics().widthPixels;
		int maxHeight = getContext().getResources().getDisplayMetrics().heightPixels;

		if (x > maxWidth / 2) {
			gravityHorz = Gravity.RIGHT;
			x = maxWidth - x;
		}
		
		if (y > maxHeight / 2) {
			gravityVert = Gravity.BOTTOM;
			y = maxHeight - y;
		}
		
		this.getWindow().getAttributes().gravity = gravityVert | gravityHorz;

		this.getWindow().getAttributes().x = x;
		this.getWindow().getAttributes().y = y;
	}

	public View getMenuItem(int position) {
		try {
			return mMenuContent.getChildAt(position * 2);
		} catch (Exception ex) {
			return null;
		}
	}
	
	/**
	 * 
	 * @param context
	 * @param widthResId
	 * @param textsId
	 * @param actionsId
	 * @param itemLayoutId
	 * @param itemTextId
	 * @param itemSepId
	 * @param backgroundId
	 */
	public CMenu(Context context, int widthResId, int textsId, int actionsId,
			int itemLayoutId, int itemTextId, int itemSepId, int backgroundId) {
		super(context, R.style.Theme_FadeDialog);

		mMenuContent = new CMenuContent(getContext(), itemLayoutId, itemTextId,
				itemSepId, backgroundId);
		mMenuContent.setContent(textsId, actionsId);
		mMenuContent.setCommonEventListener(mCommonEventListener);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		this.getWindow().setLayout(
				getContext().getResources().getDimensionPixelSize(widthResId),
				LayoutParams.WRAP_CONTENT);
		this.setCancelable(true);
		this.setCanceledOnTouchOutside(true);
		this.setContentView(mMenuContent);
	}

	private class CMenuContent extends LinearLayout {

		private int mItemLayoutResId;
		private int mItemTextResId;
		private int mItemSepResId;
		private int mBackgroundResId;

		private CommonEventListener mCommonEventListener;

		/**
		 * Handle click event
		 */
		private OnClickListener mMenuItemClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				String action = (String) v.getTag(R.id.tag_action);
				String data = (String) v.getTag(R.id.tag_data);
				if (mCommonEventListener != null)
					mCommonEventListener.onHandleCommonEvent(this, 0,
							new String[] { action, data });
			}
		};

		/**
		 * Set CommonEventListener
		 * 
		 * @param value
		 */
		public void setCommonEventListener(CommonEventListener value) {
			mCommonEventListener = value;
		}

		/**
		 * Set menu content by text, action array
		 * 
		 * @param layoutId
		 */
		public void setContent(int textsId, int actionsId) {
			// Cleart all old views
			removeAllViews();

			// Get text
			String[] arrText = getResources().getStringArray(textsId);
			String[] arrAction = getResources().getStringArray(actionsId);
			List<String> texts = Arrays.asList(arrText);
			List<String> actions = Arrays.asList(arrAction);

			// Check size
			if (texts.size() < 0)
				return;
			if (texts.size() != actions.size())
				return;

			String text, action;
			LayoutInflater layoutInflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			for (int i = 0; i < texts.size() - 1; i++) {
				text = texts.get(i);
				action = actions.get(i);

				// Add menu item
				ViewGroup view = (ViewGroup) layoutInflater.inflate(
						mItemLayoutResId, null);
				CTextView item = (CTextView) view.findViewById(mItemTextResId);
				view.removeAllViews();

				item.setText(text);
				item.setTag(R.id.tag_action, action);
				item.setTag(R.id.tag_data, text);
				item.setOnClickListener(mMenuItemClickListener);
				this.addView(item);

				// Add menu sep
				View sep = new View(getContext());
				sep.setBackgroundColor(getResources().getColor(mItemSepResId));
				this.addView(sep, LinearLayout.LayoutParams.MATCH_PARENT, 1);
			}

			// Add the last item
			text = texts.get(texts.size() - 1);
			action = actions.get(actions.size() - 1);

			ViewGroup view = (ViewGroup) layoutInflater.inflate(
					mItemLayoutResId, null);
			CTextView item = (CTextView) view.findViewById(mItemTextResId);
			view.removeAllViews();

			item.setText(text);
			item.setTag(R.id.tag_action, action);
			item.setTag(R.id.tag_data, text);
			item.setOnClickListener(mMenuItemClickListener);
			this.addView(item);
		}

		public CMenuContent(Context context) {
			super(context);
			initLayout(null);
		}

		public CMenuContent(Context context, int itemLayoutId, int itemTextId,
				int itemSepId, int backgroundId) {
			super(context);

			mItemLayoutResId = itemLayoutId;
			mItemTextResId = itemTextId;
			mItemSepResId = itemSepId;
			mBackgroundResId = backgroundId;

			initLayout(null);
		}

		/**
		 * Initialize control
		 * 
		 * @param attrs
		 */
		private void initLayout(AttributeSet attrs) {
			setOrientation(LinearLayout.VERTICAL);
			setBackgroundResource(mBackgroundResId);
		}
	}
}
